package com.tw.wi09.ws12.aps;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class APS_SMS_Receiver extends BroadcastReceiver {


		@Override
		public void onReceive(Context context, Intent intent) {
			String MD = "onReceive";
			Log.d(MD, "started");

			// ---get the SMS message passed in---
			Bundle bundle = intent.getExtras();
			String providerNumber ="";
			SmsMessage[] msgs = null;
			String str = "";

			if (bundle != null) {
				// ---retrieve the SMS message received---
				Object[] pdus = (Object[]) bundle.get("pdus");
				
				msgs = new SmsMessage[pdus.length];
				for (int i = 0; i < msgs.length; i++) {
					msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
					providerNumber = msgs[i].getOriginatingAddress();
					str += msgs[i].getMessageBody().toString();
					str += "\n";

					Log.d(MD, "providerNumber: " + providerNumber);
					Log.d(MD,
							"msgs[i].getOriginatingAddress(): "
									+ msgs[i].getOriginatingAddress());
					Log.d(MD,
							"msgs[i].getMessageBody(): "
									+ msgs[i].getMessageBody());

					// TODO Check on SMS of Provider to check if Time is in it
					// and read the time

					
					}
				// ---display the new SMS message---
				Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
				Bundle b= new Bundle();
				b.putString("Number", providerNumber);
				b.putString("msg", str);
				Intent smsIntent = new Intent("com.tw.wi09.ws12.aps.APS_SMS_RECEIVED");
				smsIntent.putExtras(b);
				peekService(context,smsIntent);
				
				}


			}
		
	}

